/*
 * Copyright (C) Michael Martin & Antonio Carrozzini (TBJD)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.tbjd.client.dndControllers.resourcePicker;

import java.util.HashMap;

import org.tbjd.client.beans.Resource;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.exceptions.TBJDException;
import org.tbjd.client.exceptions.NoUpperDecisionmakerFoundException;
import org.tbjd.client.exceptions.NoUpperMediatorFoundException;
import org.tbjd.client.guis.gantt.items.GanttTaskItem;
import org.tbjd.client.guis.resourcePicker.items.ResourcePickerItem;
import org.tbjd.client.interfaces.Decisionmaker;

import com.allen_sauer.gwt.dnd.client.DragContext;
import com.allen_sauer.gwt.dnd.client.drop.SimpleDropController;
import com.google.gwt.user.client.ui.Widget;

/**
 * <p>
 * The drop controller for a {@link org.tbjd.client.guis.resourcePicker.items.ResourcePickerItem ResourcePickerItem} of the
 * {@link org.tbjd.client.guis.resourcePicker.ResourcePickerGui ResourcePickerGui}
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 * @see org.tbjd.client.dndControllers.resourcePicker.ResourcePickerDragController
 * 
 */
public class ResourcePickerDropController extends SimpleDropController implements Decisionmaker {

	private GanttTaskItem dropTarget = null;
	private Resource resource = null;

	private HashMap<Integer, Object> map = null;

	public ResourcePickerDropController(Widget dropTarget) {
		super(dropTarget);
	}

	public void onDrop(DragContext context) {
		super.onDrop(context);
		this.dropTarget = (GanttTaskItem) context.dropController.getDropTarget();
		this.resource = ((ResourcePickerItem) context.draggable).getResource();

		decide(context);
	}

	public void decide(Object action) {
		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DROP_ADD_RESOURCEONTASK_resource, this.resource);
		this.map.put(NotificationsConstants.DROP_ADD_RESOURCEONTASK_ganttTaskItem, this.dropTarget);

		try {
			delegate(this.map);

		} catch (NoUpperDecisionmakerFoundException e) {
			e.printStackTrace();

		} catch (NoUpperMediatorFoundException e) {
			e.printStackTrace();
		}
	}

	public void handle(Object action) {
	}

	public void delegate(Object action) throws NoUpperDecisionmakerFoundException, NoUpperMediatorFoundException {
		Widget parent = this.dropTarget.getParent();
		while (parent != null && parent instanceof Decisionmaker == false) {
			parent = ((Widget) parent).getParent();
		}
		if (parent != null) {
			try {
				((Decisionmaker) parent).decide(action);
			} catch (TBJDException e) {
				e.printStackTrace();
			}
		} else {
			throw new NoUpperDecisionmakerFoundException();
		}

	}

}
